package com.example.waibao.controller;


import com.example.waibao.Component.UserHelper;
import com.example.waibao.entity.*;
import com.example.waibao.service.taskService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
public class taskController {

    @Autowired
    private ThreadPoolTaskExecutor applicationTaskExecutor;

    @Autowired
    private taskService taskService;


    @Autowired
    private UserHelper userHelper;


    @PostMapping("set_blind_box")
     public ResponseResult set_blind_box(@RequestBody box box) {

        User user = userHelper.getCurrentUser();
        box.setSchool_id(user.getSchool_id());
        taskService. set_blind_box(box);

        ResponseResult responseResult = new ResponseResult(200,"设置随机奖励成功");
        return responseResult;
    }

    @GetMapping("get_blind_box")
    public ResponseResult get_blind_box(@RequestParam(name = "page_num", defaultValue = "1")Integer page_num,
                                        @RequestParam(name = "page_size", defaultValue = "5")Integer page_size) {
        User user = userHelper.getCurrentUser();

        PageInfo<box> boxes = taskService.get_blind_box(user.getSchool_id(),page_num,page_size);

        ResponseResult responseResult = new ResponseResult(200,"获取随机奖励成功",boxes);
        return responseResult;
    }

    @PostMapping("delete_blind_box")
    public ResponseResult delete_blind_box(@RequestParam("id") int id) {
        taskService.delete_blind_box(id);
        ResponseResult responseResult = new ResponseResult(200,"删除随机奖励成功");
        return responseResult;
    }



    //如果有学生多次任务提交不通过，可以进行人工审核

    //获取要审核的任务
    @GetMapping("get_task_review")
    private ResponseResult get_task_review(int status)
    {
        User user = userHelper.getCurrentUser();
        int school_id = user.getSchool_id();
        List<task_review> taskReviews = taskService.get_task_review(school_id,status);
        ResponseResult responseResult = new ResponseResult(200,"获取要审核的任务成功",taskReviews);

        return responseResult;
    }

    //通过任务，或者拒绝通过任务
    @PostMapping("solve_task_review")
    private ResponseResult solve_task_review(@RequestBody task_review task_review) throws ParseException, ExecutionException, InterruptedException {

        System.out.println(task_review);

        //正常串行执行sql语句：

//        taskService.solve_task_review(task_review);
//      //  String time = "2017-10-19";
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//yyyy-mm-dd, 会出现时间不对, 因为小写的mm是代表: 秒
//        Date utilDate = sdf.parse(task_review.getScore_time());
//        taskService.add_score_detail(utilDate,task_review.getScore(),task_review.getTask_id(),task_review.getUser_id());
//
//        taskService.update_score_sum(task_review.getUser_id(),task_review.getScore(),utilDate);



        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//yyyy-mm-dd, 会出现时间不对, 因为小写的mm是代表: 秒
        Date utilDate = sdf.parse(task_review.getScore_time());


        taskService.solve_task_review(task_review);

        //如果通过任务则进行加分
        if(task_review.getStatus()==1) {

            //多线程异步执行sql语句，基本上能提升百分之40运行速度
//        CompletableFuture addressFuture = CompletableFuture.runAsync(()->{
//        }, applicationTaskExecutor);
            CompletableFuture addressFuture2 = CompletableFuture.runAsync(() -> {
                taskService.add_score_detail(utilDate, task_review.getScore(), task_review.getTask_id(), task_review.getUser_id());
            }, applicationTaskExecutor);
            CompletableFuture addressFuture3 = CompletableFuture.runAsync(() -> {
                taskService.update_score_sum(task_review.getUser_id(), task_review.getScore(), utilDate);
            }, applicationTaskExecutor);

            CompletableFuture addressFuture4 = CompletableFuture.runAsync(() -> {
                taskService.update_speciality_score(task_review.getScore(), task_review.getUser_id(), utilDate);
            }, applicationTaskExecutor);
            //学生等级升级
//            CompletableFuture addressFuture5 = CompletableFuture.runAsync(() -> {
//                taskService.update_student_level(task_review.getUser_id());
//            }, applicationTaskExecutor);

//        CompletableFuture.allOf(addressFuture,addressFuture2,addressFuture3,addressFuture4).get();
            CompletableFuture.allOf(addressFuture2, addressFuture3, addressFuture4).get();
            ResponseResult responseResult = new ResponseResult(200,"通过任务成功");
            return responseResult;
        }else if(task_review.getStatus()==2) {
            ResponseResult responseResult = new ResponseResult(200,"驳回任务成功");
            return responseResult;
        }else {
            ResponseResult responseResult = new ResponseResult(400,"任务审核错误:status参数错误");
            return responseResult;
        }
    }


    //获取学生的任务进度
    @GetMapping("get_student_process")
    public ResponseResult2 get_student_process(@RequestParam("user_id") int user_id)
    {

        User user = userHelper.getCurrentUser();
        int school_id = user.getSchool_id();
        List<task> student_tasks = taskService.get_student_process1(user_id);
        //LinkedHashSet<task> student_tasks = new LinkedHashSet<>(student_tasks2);
        List<task> all_tasks = taskService.get_student_process2(user_id,school_id);

        ResponseResult2 responseResult2 = new ResponseResult2(200,"获取学生任务进度成功",student_tasks,all_tasks);
        return responseResult2;
    }

}
